unit UnitBookFrame;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, GSPInterface, UnitBaseFrame, GradientTabSet, UnitGlobal, ComCtrls,
  IniFiles, StdCtrls;

type
  TFrameBook = class(TFrameBase)
    gtsBook: TGradientTabSet;
    lvUsers: TListView;
    procedure gtsBookMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure gtsBookGetImageIndex(Sender: TObject; TabIndex: Integer;
      var ImageIndex: Integer);
    procedure FrameResize(Sender: TObject);
  private
    FSelected: Boolean;
    FFileName: string;
    FDatabase: IGSPDatabase;
    procedure SetSelected(const Value: Boolean);
    procedure SetDatabase(const Value: IGSPDatabase);
    procedure WMNCHitTest(var Message: TWMNCHitTest); message WM_NCHITTEST;
  protected
  public
    constructor Create(AOwner: TComponent); override;
    procedure Update;
    function GetSelectedUser: IGSPRecord;
    function AddUser(AUser: IGSPRecord): TListItem;
    function RemoveUser(AUser: IGSPRecord): Boolean;
    property Database: IGSPDatabase read FDatabase write SetDatabase;
    property FileName: string read FFileName write FFileName;
    property Selected: Boolean read FSelected write SetSelected;
  end;

var
  FrameBook: TFrameBook;

implementation

{$R *.dfm}

{ TFrameBook }

function TFrameBook.AddUser(AUser: IGSPRecord): TListItem;
begin
  Assert(Assigned(AUser));
  Result := lvUsers.Items.Add;
  Result.Caption := AUser.AsString[pfnUserName];
  Result.ImageIndex := AUser.AsInteger[pfnSex];
  if AUser.AsBoolean[pfnIsCreator] then
    Result.ImageIndex := Result.ImageIndex + 2;
  Result.Data := Pointer(AUser);
end;

constructor TFrameBook.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  gtsBook.Scroller.Hide;
end;

procedure TFrameBook.FrameResize(Sender: TObject);
begin
  inherited;
  lvUsers.Realign;
end;

function TFrameBook.GetSelectedUser: IGSPRecord;
begin
  Result := nil;
  if Assigned(lvUsers.Selected) then
    Result := IGSPRecord(lvUsers.Selected.Data);
end;

procedure TFrameBook.gtsBookGetImageIndex(Sender: TObject; TabIndex: Integer;
  var ImageIndex: Integer);
begin
  ImageIndex := 7;
end;

procedure TFrameBook.gtsBookMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  SetFocus;
end;

function TFrameBook.RemoveUser(AUser: IGSPRecord): Boolean;
var
  I: Integer;
begin
  Assert(Assigned(AUser));
  Result := False;
  for I := 0 to lvUsers.Items.Count - 1 do
    if IGSPRecord(lvUsers.Items[I].Data) = AUser then
    begin
      lvUsers.Items[I].Delete;
      Result := True;
      Break;
    end;
end;

procedure TFrameBook.SetDatabase(const Value: IGSPDatabase);
var
  I: Integer;
  iUsers: IGSPTable;
begin
  lvUsers.Clear;
  gtsBook.Tabs.Clear;
  FDatabase := Value;
  if FDatabase = nil then Exit;
  gtsBook.Tabs.Text := FDatabase.DisplayName;
  gtsBook.TabIndex := 0;
  iUsers := FDatabase.FindTable(ptnUsers);
  for I := 0 to iUsers.RecordCount - 1 do
    AddUser(iUsers[I]);
end;

procedure TFrameBook.SetSelected(const Value: Boolean);
begin
  if FSelected <> Value then
  begin
    FSelected := Value;
    if FSelected then
    begin
      gtsBook.TabColor.ActiveBegin := clActiveBegin;
      gtsBook.TabColor.ActiveEdge := clActiveCaption;
      //lvUsers.Selected := lvUsers.ItemFocused;
    end
    else begin
      gtsBook.TabColor.ActiveBegin := clBtnFace;
      gtsBook.TabColor.ActiveEdge := clBtnShadow;
      lvUsers.Selected := nil;
      lvUsers.ItemFocused := nil;
    end;
  end;
end;

procedure TFrameBook.Update;
begin
  SetDatabase(FDatabase);
end;

procedure TFrameBook.WMNCHitTest(var Message: TWMNCHitTest);
begin
  Message.Result := HTBOTTOM;
end;

end.
